package kt.com.bilin.coin.utils

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.yanzhenjie.album.AlbumFile
import com.yanzhenjie.album.AlbumLoader
import com.youth.banner.loader.ImageLoader
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import kt.com.bilin.coin.common.dp

object GlideUtils {
    /**
     * 加载圆形
     */
    fun intoCircle(tag: Any, url: Any?, imageView: ImageView, placeholder: Int? = null) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            requestBuilder.apply(RequestOptions.circleCropTransform())
            requestBuilder.into(imageView)
        }
    }
    // Glide.with(ivImage.context).load(bean.filePath ?: bean.url ?: bean.resId).apply(RequestOptions().transform(CenterCrop(), RoundedCorners(SizeUtils.dp2px(10f)))).into(ivImage)
    /**
     * 加载圆角
     */
    fun intoRoundedCorners(tag: Any, url: Any?, imageView: ImageView, placeholder: Int? = null, roundingRadius: Int? = null) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            if (roundingRadius != null) requestBuilder.apply(RequestOptions().transform(CenterCrop(), RoundedCorners(roundingRadius.dp)))
            requestBuilder.into(imageView)
        }
    }

    /**
     * 加载圆角,设置那个方向需要圆角
     */
    fun intoRoundedCorners(tag: Any, url: Any?, imageView: ImageView, placeholder: Int? = null, roundingRadius: Int? = 0, cornerType: RoundedCornersTransformation.CornerType) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            if (roundingRadius != null) requestBuilder.apply(RequestOptions().transform(CenterCrop(), RoundedCornersTransformation(roundingRadius.dp, 0, cornerType)))
            requestBuilder.into(imageView)
        }
    }

    fun intoView(tag: Any, url: Any?, imageView: ImageView, placeholder: Int? = null) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            requestBuilder.into(imageView)
        }
    }

    private fun withRequestManager(tag: Any): RequestManager? {
        return when (tag) {
            is View -> Glide.with(tag)
            is Context -> Glide.with(tag)
            is Activity -> Glide.with(tag)
            is androidx.fragment.app.Fragment -> Glide.with(tag)
            is FragmentActivity -> Glide.with(tag)
            is Fragment -> Glide.with(tag)
            else -> {
                Logger.e("tag不合法")
                null
            }
        }

    }
}

class GlideImageLoader : ImageLoader() {
    override fun displayImage(context: Context, path: Any, imageView: ImageView) {
        Glide.with(context)
                .load(path) //                .apply(new RequestOptions().transform(new CenterCrop(),new RoundedCorners(10)))
                .into(imageView)
    }
}

class GlideAlbumLoader : AlbumLoader {
    override fun load(imageView: ImageView, albumFile: AlbumFile) {
        load(imageView, albumFile.path)
    }

    override fun load(imageView: ImageView, imagePath: String) {
        Glide.with(imageView.context)
                .load(imagePath)
                .into(imageView)
    }
}